package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.PublicCustomersMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.dao.VipCustomerMapper;
import com.hqyj.pojo.DataMessage;
import com.hqyj.pojo.PublicCustomers;
import com.hqyj.pojo.User;
import com.hqyj.pojo.VipCustomer;
import com.hqyj.service.PublicCustomerService;
import com.hqyj.service.VipCustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class PublicCustomerServiceImpl implements PublicCustomerService {
    @Resource
    PublicCustomersMapper publicCustomersMapper;
    @Resource
    VipCustomerMapper vipCustomerMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public DataMessage queryAll(PublicCustomers publicCustomers) {
        PageHelper.startPage(publicCustomers.getPage(),publicCustomers.getRow());
        List<PublicCustomers> list = publicCustomersMapper.queryAll(publicCustomers);

        //全体vip客户的人数
        int vipCustomerNumber = vipCustomerMapper.countAllVipCustomer();
        //性别为男性的人数
        int vipCustomerSexManNumber = vipCustomerMapper.countVipCustomerSexManNumber();
        //男性概率
        double manProbability = (double)vipCustomerSexManNumber/(double)vipCustomerNumber;
        //女性概率
        double womanProbability = 1 - manProbability;
        //获取性别权重
        double sex;
        sex = Math.abs(manProbability - womanProbability);
        //线索来源为线上的人数
        int vipCustomerOnlineNumber = vipCustomerMapper.countVipCustomerOnlineNumber();
        //线索来源为线上的概率
        double onlineProbability = (double)vipCustomerOnlineNumber/(double)vipCustomerNumber;
        //线索来源为线下的概率
        double offOnlineProbability = 1 - onlineProbability;
        //获取线索来源是线上还是线下权重
        double line;
        line = Math.abs(onlineProbability - offOnlineProbability);
        //收入水平为高收入的人数
        int vipCustomerHighNumber = vipCustomerMapper.countVipCustomerHighNumber();
        //收入水平为高收入的概率
        double highProbability = (double)vipCustomerHighNumber/(double)vipCustomerNumber;
        //收入水平为一般收入的概率
        double lowProbability = 1 - highProbability;
        //获取获取收入水平高还是低权重
        double money;
        money = Math.abs(highProbability - lowProbability);
        //已婚人数
        int vipCustomerMarriedNumber = vipCustomerMapper.countVipCustomerMarriedNumber();
        //已婚概率
        double marriedProbability = (double)vipCustomerMarriedNumber/(double)vipCustomerNumber;
        //未婚概率
        double singleProbability = 1 - marriedProbability;
        //获取婚姻状况是低还是高权重
        double marry;
        marry = Math.abs(marriedProbability - singleProbability);
        double successfulProbability = 0;
        //查询所有
        for (PublicCustomers p:list
        ) {

            if(p.getPublicCustomersSex().equals("男")){
                successfulProbability = successfulProbability + (manProbability*sex);
            }else {
                successfulProbability = successfulProbability + ((1-manProbability)*sex);
            }

            if(p.getCueSource().equals("线上")){
                successfulProbability = successfulProbability + (onlineProbability*line);
            }else {
                successfulProbability = successfulProbability + ((1-onlineProbability)*line);
            }

            if(p.getIncomeLevel().equals("高收入")){
                successfulProbability = successfulProbability + (highProbability*money);
            }else {
                successfulProbability = successfulProbability + ((1-highProbability)*money);
            }

            if(p.getMaritalStatus().equals("已婚")){
                successfulProbability = successfulProbability + (marriedProbability*marry);
            }else {
                successfulProbability = successfulProbability + ((1-marriedProbability)*marry);
            }


            BigDecimal two = new BigDecimal(successfulProbability);
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            p.setSuccessfulProbability(three);
        }
        DataMessage data = new DataMessage();
        //创建分页结果集
        PageInfo<PublicCustomers> pageResult = new PageInfo<>(list);
        //判断分页结果是否有值
        if(pageResult != null){
            Map<String,Object> map = new HashMap<>();
            //当前页集合
            map.put("list",pageResult.getList());
            //总页数
            map.put("totalPage",pageResult.getPages());
            //总条数
            map.put("total",pageResult.getTotal());
            System.out.println("map"+map);
            data.setSuccess(true);
            data.setData(map);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }

        return data;
    }

    @Override
    public DataMessage add(PublicCustomers publicCustomers) {
        DataMessage data = new DataMessage();

        int num = publicCustomersMapper.insert(publicCustomers);
        if (num > 0){
            data.setSuccess(true);
            data.setInfo("添加员工成功");
        }else {
            data.setSuccess(false);
            data.setInfo("添加员工失败");
        }
        return data;
    }

    @Override
    public DataMessage updateUsrName(PublicCustomers publicCustomers) {
        DataMessage data = new DataMessage();
        String arr[] = publicCustomers.getBitId().split(",");
        List<String> list = Arrays.asList(arr);
        HashMap <String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("userName",publicCustomers.getUserName());
        int num = publicCustomersMapper.updateUserName(map);
        if (num > 0){
            data.setSuccess(true);
            data.setInfo("批量更改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("批量更改失败");
        }
        return data;
    }

//    @Override
//    public DataMessage setSuccessfulProbability(PublicCustomers p) {
//        DataMessage data = new DataMessage();
//
//        if(p.getPublic_customers_sex().equals("男")){
//            successfulProbability = successfulProbability + (manProbability*sex);
//        }else {
//            successfulProbability = successfulProbability + ((1-manProbability)*sex);
//        }
//
//        if(p.getCue_source().equals("线上")){
//            successfulProbability = successfulProbability + (onlineProbability*line);
//        }else {
//            successfulProbability = successfulProbability + ((1-onlineProbability)*line);
//        }
//
//        if(p.getIncome_level().equals("高收入")){
//            successfulProbability = successfulProbability + (highProbability*money);
//        }else {
//            successfulProbability = successfulProbability + ((1-highProbability)*money);
//        }
//
//        if(p.getMarital_status().equals("已婚")){
//            successfulProbability = successfulProbability + (marriedProbability*marry);
//        }else {
//            successfulProbability = successfulProbability + ((1-marriedProbability)*marry);
//        }
//
//
//        BigDecimal two = new BigDecimal(successfulProbability);
//        double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//
//        return three;
//        //全体vip客户的人数
//        int vipCustomerNumber = vipCustomerDao.countAllVipCustomer();
//
//        //性别为男性的人数
//        int vipCustomerSexManNumber = vipCustomerDao.countVipCustomerSexManNumber();
//        //男性概率
//        double manProbability = (double)vipCustomerSexManNumber/(double)vipCustomerNumber;
//        //女性概率
//        double womanProbability = 1 - manProbability;
//        //获取性别权重
//        double sex;
//        sex = Math.abs(manProbability - womanProbability);
//
//
//        //线索来源为线上的人数
//        int vipCustomerOnlineNumber = vipCustomerDao.countVipCustomerOnlineNumber();
//        //线索来源为线上的概率
//        double onlineProbability = (double)vipCustomerOnlineNumber/(double)vipCustomerNumber;
//        //线索来源为线下的概率
//        double offOnlineProbability = 1 - onlineProbability;
//        //获取线索来源是线上还是线下权重
//        double line;
//        line = Math.abs(onlineProbability - offOnlineProbability);
//        return three;
//    }


}
